package org.canx.config;

import org.canx.service.TokenService;
import org.canx.utils.ConstantCommon;
import org.canx.utils.Response;
import org.canx.utils.ResultHttpStatus;
import com.alibaba.fastjson.JSON;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @author Andrew Qian
 * @class description :
 * @time 2017年7月18日 下午2:01:50
 */
@Component
public class SessionInterceptor extends HandlerInterceptorAdapter {

    private static Logger logger = Logger.getLogger(SessionInterceptor.class);

    @Autowired
    public TokenService tokenService;

    /**
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {

        Response res = new Response();
        res.setStatus(ResultHttpStatus.NO_LOGIN.getValue());
        res.setMsg("login time out");
        PrintWriter out = response.getWriter();
        out.print(JSON.toJSONString(res));
        out.flush();
        return false;
    }


     @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
     throws Exception {
     //前端将token信息保存在 request的header中
     //每次请求带上header中token信息
     String token = request.getHeader("token");
     if(!"OPTIONS".equals(request.getMethod())) {

     if(token != null) {
     return true;
     }
     Response res = new Response();
     res.setStatus(ResultHttpStatus.NO_LOGIN.getValue());
     res.setMsg("login time out");
     PrintWriter out = response.getWriter();
     out.print(JSON.toJSONString(res));
     out.flush();
     return false;
     }
     return true;
     }
     */


    // cookie方案
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        //前端将token信息保存在cookie中
        //每次请求带上cookie中token信息
        Cookie cookie = WebUtils.getCookie(request, "P-TOKEN");
        String token = cookie != null ? cookie.getValue() : null;
        if (!"OPTIONS".equals(request.getMethod())) {

            if (token != null) {
                if(tokenService.checkTokenIsExist(token) >= 2)
                    return true;
            }
            //跨域设置
            response.addHeader("Access-Control-Allow-Origin", ConstantCommon.CROSS_DOMAIN_URL);
            response.addHeader("Access-Control-Allow-Credentials", "true");
            response.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT, HEAD");
            response.addHeader("Access-Control-Allow-Headers", "Content-Type");
            response.addHeader("Access-Control-Max-Age", "3600");

            Response res = new Response();
            res.setStatus(ResultHttpStatus.NO_LOGIN.getValue());
            res.setMsg(ResultHttpStatus.NO_LOGIN.getName());
            PrintWriter out = response.getWriter();
            out.print(JSON.toJSONString(res));
            out.flush();
            return false;
        }
        return true;
    }
}
